/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：UserAction.java
 * History: Dec 20, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.entities.BaseGoodsCollection;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.BaseMemberInfo;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.entities.BasePasswordQuestion;
import com.xteam.ecsystem.entities.SysWebConfig;
import com.xteam.ecsystem.service.front.FrontGoodsCollectionService;
import com.xteam.ecsystem.service.front.FrontOrderService;
import com.xteam.ecsystem.service.front.FrontUserService;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.ecsystem.utils.AppConfig;
import com.xteam.ecsystem.utils.CookiesUtils;
import com.xteam.ecsystem.utils.FrontUtil;
import com.xteam.ecsystem.utils.JavaMailUtil;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端用户基本请求控制层
 * @author liuzhi
 */
@Controller
public class UserAction {
	
	@Autowired
	private FrontUserService userService;
	
	@Autowired
	private FrontOrderService orderService;
	
	@Autowired
	private FrontGoodsCollectionService userFavService;
	
	@Autowired
	private ConfigManager cfgManager;
	
	/**
	 * 设置响应类型
	 * @author liuzhi Feb 6, 2013
	 * @param request
	 * @param response
	 */
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	}
	
	/**
	 * 前往登录页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/login.xhtml", method = RequestMethod.GET)
	public String toLogin(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 用户登录
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/login.xhtml", method = RequestMethod.POST)
	public void login(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status = "succ";
		String emailOrUserName = FrontUtil.getStringParam(request, "emailOrUserName", "");
		String bmbPassword = FrontUtil.getStringParam(request, "bmbPassword", "");
		
		BaseMemberBase bmbObj = this.userService.getBMBByEmailAPwd(emailOrUserName, bmbPassword);
		
		if (bmbObj != null) {
			request.getSession().setAttribute(Constant.VAR_MEMBER.value(), bmbObj);
			
			bmbObj.setBmbLast(new Date());
			this.userService.saveOrUpdateBMB(bmbObj, bmbObj.getBmbId());
			
			// 保存未登录前，在cookie中自动生成的用户ID
			String cookieUserId = "";
			Cookie cookie = CookiesUtils.getCookieByName(request, "XMECS_USERID");
			
			if(cookie != null){
				cookieUserId = java.net.URLDecoder.decode(cookie.getValue(), "UTF-8");
				
				// 将购物车中的游客身份修改为登录用户身份
				this.userService.updateUserStatus(bmbObj.getBmbId(), cookieUserId);
			}
		}else{
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 登录成功，执行页面跳转
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/loginSucc.xhtml", method = RequestMethod.GET)
	public String loginSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 登录失败，执行页面跳转
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/loginFail.xhtml", method = RequestMethod.GET)
	public String loginFail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 用户登出
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/logout.xhtml", method = RequestMethod.POST)
	public void logout(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status = "succ";
		request.getSession().removeAttribute(Constant.VAR_MEMBER.value());
		request.getSession().invalidate();
		
		response.getWriter().print(status);
	}
	
	/**
	 * 退出成功，执行页面跳转
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/logoutSucc.xhtml", method = RequestMethod.GET)
	public String logoutSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往邮件找回密码页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdByEmail.xhtml", method = RequestMethod.GET)
	public String getPwdByEmail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 发送密码找回邮件
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdByEmail.xhtml", method = RequestMethod.POST)
	public void getPwdByEmail_sendMail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String bmbNick = FrontUtil.getStringParam(request, "bmbNick", "");
		String bmbEmail = FrontUtil.getStringParam(request, "bmbEmail", "");
		String status = "succ";
		String message = "";
		
		// 针对账户和邮箱进行验证
		BaseMemberBase bmbObj = this.userService.getBMBByNickAEmail(bmbNick, bmbEmail);
		
		if (bmbObj == null) {
			// 账户和邮箱不匹配
			status = "fail_1";
		}else{
			// 密码重置邮件发送环节
			JavaMailUtil mailTool = new JavaMailUtil();
			SysWebConfig webConfig = this.cfgManager.getWebConfig();
			
			mailTool.setHostname(webConfig.getSwcMhostname());
			mailTool.setUsername(webConfig.getSwcMusername());
			mailTool.setPassword(webConfig.getSwcMpassword());
			mailTool.setMail_from(webConfig.getSwcMusername());
			mailTool.setMail_to(bmbEmail);
			mailTool.setPersonName(webConfig.getSwcMdispname());
			mailTool.setMail_subject(webConfig.getSwcPwdsubject());
			
			String port = "";
			int serverPort = request.getServerPort();
			if (serverPort != 80) port = ":" + serverPort;
			
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://" + request.getServerName() + port + path + "/";
			mailTool.setMail_content_type("application/x-foobar");
			
			// 生成激活码
			String actCode = UUID.randomUUID().toString().replace("-", "");
			// 获得密码重置邮件内容
			String sendContent = webConfig.getSwcPwdcontent();
			sendContent = sendContent.replace("@newLine", "\n");
			sendContent = sendContent.replace("@targetMail", bmbEmail);
			sendContent = sendContent.replace("@targetResetUrl", basePath + "user/resetPwd.xhtml?actCode="+ actCode);
			mailTool.setMail_content(sendContent);
			
			// 发送密码重置邮件
			boolean isSucc = mailTool.sendNormalMail();
			
			if (isSucc) {
				bmbObj.setBmbActivator(actCode);
				
				// 更新会员信息
				this.userService.saveOrUpdateBMB(bmbObj, bmbObj.getBmbId());
				
				// 获得用户邮箱后缀并生成指定的邮箱链接
				String suffix = bmbEmail.substring(bmbEmail.lastIndexOf("@") + 1);
				if (AppConfig.specialEmails.containsKey(suffix)) {
					message = AppConfig.specialEmails.get(suffix);
				}else{
					message = "mail." + suffix;
				}
			}else{
				status = "fail_2";
			}
		
		}
		
		//回传发送结果
		JSONObject data = new JSONObject();
		data.put("status", status);
		data.put("message", message);
		
		response.getWriter().print(data);
	}
	
	/**
	 * 前往邮件地址找回密码失败页面（用户名和邮件地址不匹配）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdFail_1.xhtml", method = RequestMethod.GET)
	public String getPwdFail_1(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往邮件地址找回密码失败页面（密码找回邮件发送失败）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdFail_2.xhtml", method = RequestMethod.GET)
	public String getPwdFail_2(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往邮件地址找回密码成功
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/sendPRESucc.xhtml", method = RequestMethod.GET)
	public String sendPRESucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			model.put(Constant.VAR_EMAILURL_GB.value(), FrontUtil.getStringParam(request, "emailUrl", ""));
		}
		
		return template;
	}
	
	/**
	 * 重置密码（通过邮件地址访问）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/resetPwd.xhtml", method = RequestMethod.GET)
	public String resetPwd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String actCode = FrontUtil.getStringParam(request, "actCode", "");
		String status = "succ";
		
		BaseMemberBase bmbObj = this.userService.getBMBByActCode(actCode);
		
		if (bmbObj != null) {
			bmbObj.setBmbActivator(null);
			bmbObj.setBmbState(1);
			
			// 更新用户信息
			this.userService.saveOrUpdateBMB(bmbObj, bmbObj.getBmbId());
			model.put(Constant.VAR_MEMBERID.value(), bmbObj.getBmbId());
		}else{
			status = "fail";
		}
		
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			model.put(Constant.VAR_PWDRESETMSG.value(), status);
		}
		
		return template;
	}
	
	/**
	 * 设置新密码
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/setNewPwd.xhtml", method = RequestMethod.POST)
	public void setNewPwd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String status ="succ";
		String newPwd = FrontUtil.getStringParam(request, "newPwd", "");
		String bmbId = FrontUtil.getStringParam(request, "bmbId", "");
		
		BaseMemberBase bmbObj = this.userService.getBMBById(bmbId);
		
		if (bmbObj != null) {
			bmbObj.setBmbPassword(newPwd);
			
			this.userService.saveOrUpdateBMB(bmbObj, bmbId);
		}else{
			status ="fail";
		}
		
		response.getWriter().print(status);
	}
	
	
	/**
	 * 前往新密码设置成功页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/setNewPwdSucc.xhtml", method = RequestMethod.GET)
	public String setNewPwdSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往新密码设置失败页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/setNewPwdFail.xhtml", method = RequestMethod.GET)
	public String setNewPwdFail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往密保问题找回密码页面 - 1
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdByQues.xhtml", method = RequestMethod.GET)
	public String getPwdByQues(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 获取注册用户设置的密保问题
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdQues.xhtml", method = RequestMethod.POST)
	public void getPwdQues(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status ="succ";
		String bmbId = "";
		String bmbquestion = "";
		String bmbNick = FrontUtil.getStringParam(request, "bmbNick", "");
		
		BaseMemberBase bmbObj = this.userService.getBMBByNick(bmbNick);
		
		if (StringUtil.isNullOrEmpty(bmbObj) || StringUtil.isNullOrEmpty(bmbObj.getBmbQuestion())) {
			status ="fail";
		}else{
			bmbId = bmbObj.getBmbId();
			bmbquestion = bmbObj.getBmbQuestion();
		}
		
		//回传发送结果
		JSONObject data = new JSONObject();
		data.put("status", status);
		data.put("bmbId", bmbId);
		data.put("bmbquestion", bmbquestion);
		
		response.getWriter().print(data);
	}
	
	/**
	 * 前往密保问题找回密码页面 - 2
	 * @author liuzhi Feb 5, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdQues.xhtml", method = RequestMethod.GET)
	public String getPwdQues_2(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String bmbId = FrontUtil.getStringParam(request, "bmbId", "");
		String bmbquestion = FrontUtil.getStringParam(request, "bmbquestion", "");
		
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			// 获得问题实体对象
			BasePasswordQuestion bpqObj = this.userService.getBpqById(bmbquestion);
			
			model.put(Constant.VAR_MEMBERID.value(), bmbId);
			model.put(Constant.VAR_QUES_CONTENT.value(), bpqObj.getBpqContent());
		}
		
		return template;
	}
	
	/**
	 * 验证密保问题答案
	 * @author liuzhi Feb 5, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/validAnswer.xhtml", method = RequestMethod.POST)
	public void validAnswer(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String bmbId = FrontUtil.getStringParam(request, "bmbId", "");
		String bmbAnswer = FrontUtil.getStringParam(request, "bmbAnswer", "");
		String status = "";
		
		BaseMemberBase bmbObj = this.userService.getBMBByBmbAnswer(bmbId, bmbAnswer);
		
		if(bmbObj != null){
			status = "succ";
		}else{
			status = "fail";
		}
		
		//回传发送结果
		JSONObject data = new JSONObject();
		data.put("status", status);
		data.put("bmbId", bmbId);
		
		response.getWriter().print(data);
	}
	
	/**
	 * 前往密保问题找回密码失败页面（注册用户或密保问题不存在）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdFail_3.xhtml", method = RequestMethod.GET)
	public String getPwdFail_3(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 前往密保问题找回密码失败页面（输入的密保答案不正确）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/getPwdFail_4.xhtml", method = RequestMethod.GET)
	public String getPwdFail_4(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 重置密码（通过密保问题访问）
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/resetPwd_2.xhtml", method = RequestMethod.GET)
	public String resetPwd_2(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String bmbId = FrontUtil.getStringParam(request, "bmbId", "");
		String status = FrontUtil.getStringParam(request, "status", "");
		
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			model.put(Constant.VAR_MEMBERID.value(), bmbId);
			model.put(Constant.VAR_PWDRESETMSG.value(), status);
		}
		
		return template;
	}
	
	/**
	 * 前往注册页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/reg.xhtml", method = RequestMethod.GET)
	public String toRegister(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
		}
		
		return template;
	}
	
	/**
	 * 检查用户名是否被占用
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/chkNick.xhtml", method = RequestMethod.POST)
	public void chkNick(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status = "succ";
		String bmbNick = FrontUtil.getStringParam(request, "bmbNick", "");
		
		BaseMemberBase bmbObj = this.userService.getBMBByNick(bmbNick);
		
		if (bmbObj != null) {
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 检查用户名是否被占用
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/chkEmail.xhtml", method = RequestMethod.POST)
	public void chkEmail(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String status = "succ";
		String bmbEmail = FrontUtil.getStringParam(request, "bmbEmail", "");
		
		BaseMemberBase bmbObj = this.userService.getBMBByEmail(bmbEmail);
		
		if (bmbObj != null) {
			status = "fail";
		}
		
		response.getWriter().print(status);
	}
	
	/**
	 * 用户注册
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/user/reg.xhtml", method = RequestMethod.POST)
	public void register(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String message = "";
		String bmbNick = FrontUtil.getStringParam(request, "bmbNick", "");
		String bmbEmail = FrontUtil.getStringParam(request, "bmbEmail", "");
		String bmbMobile = FrontUtil.getStringParam(request, "bmbMobile", "");
		String bmbPassword = FrontUtil.getStringParam(request, "bmbPassword", "");
		String bmbQuestion = FrontUtil.getStringParam(request, "bmbQuestion", "");
		String bmbAnswer = FrontUtil.getStringParam(request, "bmbAnswer", "");
		int bmbSmnotify = FrontUtil.getIntegerParam(request, "bmbSmnotify", 0);
		int bmbEmnotify = FrontUtil.getIntegerParam(request, "bmbEmnotify", 0);
		String dataArray = FrontUtil.getStringParam(request, "dataArray", "");
		// actDir区分重定向方向
		String actDir = FrontUtil.getStringParam(request, "actDir", "");
		
		JSONArray jsonArray = null;
		
		if(dataArray != null && dataArray != ""){
			jsonArray = JSONArray.fromObject(dataArray);
		}
		
		// 激活邮件发送环节
		JavaMailUtil mailTool = new JavaMailUtil();
		SysWebConfig webConfig = this.cfgManager.getWebConfig();
		
		mailTool.setHostname(webConfig.getSwcMhostname());
		mailTool.setUsername(webConfig.getSwcMusername());
		mailTool.setPassword(webConfig.getSwcMpassword());
		mailTool.setMail_from(webConfig.getSwcMusername());
		mailTool.setMail_to(bmbEmail);
		mailTool.setPersonName(webConfig.getSwcMdispname());
		mailTool.setMail_subject(webConfig.getSwcActsubject());
		
		String port = "";
		int serverPort = request.getServerPort();
		if (serverPort != 80) port = ":" + serverPort;
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + port + path + "/";
		mailTool.setMail_content_type("application/x-foobar");
		
		// 生成激活码
		String actCode = UUID.randomUUID().toString().replace("-", "");
		// 获得激活邮件内容
		String sendContent = webConfig.getSwcActcontent();
		sendContent = sendContent.replace("@newLine", "\n");
		sendContent = sendContent.replace("@targetMail", bmbEmail);
		sendContent = sendContent.replace("@targetResetUrl", basePath + "user/actUser.xhtml?actCode="+ actCode + "&actDir=" + actDir);
		mailTool.setMail_content(sendContent);
		
		// 发送激活邮件
		boolean isSucc = mailTool.sendNormalMail();
		
		if (isSucc) {
			BaseMemberBase bmbObj = new BaseMemberBase();
			bmbObj.setBmbNick(bmbNick);
			bmbObj.setBmbEmail(bmbEmail);
			bmbObj.setBmbMobile(bmbMobile);
			bmbObj.setBmbPassword(bmbPassword);
			bmbObj.setBmbQuestion(bmbQuestion);
			bmbObj.setBmbAnswer(bmbAnswer);
			bmbObj.setBmbLast(null);
			bmbObj.setBmbState(0);
			bmbObj.setBmbIntegral(0);
			bmbObj.setBmbSmnotify(bmbSmnotify);
			bmbObj.setBmbEmnotify(bmbEmnotify);
			bmbObj.setBmbActivator(actCode);
			
			// 保存会员信息
			String bmbId = this.userService.saveOrUpdateBMB(bmbObj, null);
			
			if (StringUtil.isNotNullOrEmpty(bmbId)) {
				if(jsonArray != null && jsonArray.size() > 0){
					BaseMemberInfo bmiObj = null;
					List listObj = new ArrayList();
					
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObj = jsonArray.getJSONObject(i);
						
						bmiObj = new BaseMemberInfo();
						bmiObj.setBmiMember(bmbId);
						bmiObj.setBmiItem(jsonObj.getString("bmiItem"));
						bmiObj.setBmiValue(jsonObj.getString("bmiValue"));
						
						listObj.add(bmiObj);
					}
					
					// 保存会员资料项信息
					this.userService.saveMuchObj(listObj);
				}
				
				// 获得用户邮箱后缀并生成指定的邮箱链接
				String suffix = bmbEmail.substring(bmbEmail.lastIndexOf("@") + 1);
				if (AppConfig.specialEmails.containsKey(suffix)) {
					message = AppConfig.specialEmails.get(suffix);
				}else{
					message = "mail." + suffix;
				}
			}
		}
		
		response.getWriter().print(message);
	}
	
	/**
	 * 前往注册成功页面
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/regSucc.xhtml", method = RequestMethod.GET)
	public String regSucc(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			model.put(Constant.VAR_EMAILURL_RE.value(), FrontUtil.getStringParam(request, "emailUrl", ""));
		}
		
		return template;
	}
	
	/**
	 * 激活注册用户
	 * @author liuzhi Jan 25, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/user/actUser.xhtml", method = RequestMethod.GET)
	public String actUser(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String actCode = FrontUtil.getStringParam(request, "actCode", "");
		String actDir = FrontUtil.getStringParam(request, "actDir", "");
		String status = "succ";
		
		BaseMemberBase bmbObj = this.userService.getBMBByActCode(actCode);
		
		if (bmbObj != null) {
			bmbObj.setBmbActivator(null);
			bmbObj.setBmbState(1);
			
			// 更新用户信息
			this.userService.saveOrUpdateBMB(bmbObj, bmbObj.getBmbId());
		}else{
			status = "fail";
		}
		
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			model.put(Constant.VAR_ACTUSERMSG.value(), status);
			model.put(Constant.VAR_ACTDIR.value(), actDir);
		}
		
		return template;
	}
	
	
	@RequestMapping(value = "/user/orderQuery.xhtml", method = RequestMethod.POST)
	public void orderQuery(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		// 设置响应类型
		setToBeJsonType(request, response);
		
		String boiOsn = FrontUtil.getStringParam(request, "boiOsn", "");

		BaseOrderInfo orderInfo = this.orderService.getOrderByBoiOsn(boiOsn);
		
		if(orderInfo != null){
			response.getWriter().print(JSONObject.fromObject(orderInfo).toString());
		}else{
			response.getWriter().print("");
		}
		
	}
	
	@RequestMapping(value = "/user/addToCollect.xhtml", method = RequestMethod.POST)
	public void addToCollect(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		String goodsId = FrontUtil.getStringParam(request, "goodsId", "");
		String message = "";

		// 获得会话中的用户信息
		BaseMemberBase bmbObj = (BaseMemberBase)request.getSession().getAttribute(Constant.VAR_MEMBER.value());
		
		if(bmbObj != null){
			BaseGoodsCollection bgcObj = this.userFavService.getUserFav(bmbObj.getBmbId(), goodsId);
			
			if(bgcObj == null){
				bgcObj = new BaseGoodsCollection();
				bgcObj.setBgcMember(bmbObj.getBmbId());
				bgcObj.setBgcGoods(goodsId);
				bgcObj.setBgcTime(new Date());
				
				this.userFavService.saveUserFav(bgcObj);
				message = "您已成功将该商品添加至收藏夹。";
			}else{
				message = "您已收藏过该商品，无需重复收藏。";
			}
		}else{
			message = "由于您还没有登录，因此您还不能使用该功能。";
		}
		
		response.getWriter().print(message);
	}
	
}
